package com.tgjorgoski.sidekicknotes.browser;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.event.PopupMenuEvent;
import javax.swing.text.Position;
import javax.swing.text.Position.Bias;

import skt.swing.search.FindAction;

public class BrowserFindAction extends FindAction {

	private String lastSearchText;
	private BrowserSubPanel bPanel;
	
	public BrowserFindAction(boolean ignoreCase) {
		super(ignoreCase);
	}
	
	@Override
    protected void initSearch(ActionEvent ae){
		bPanel=null;
		if (lastSearchText==null) {
	        searchField.setText(""); //NOI18N
	        searchField.setForeground(Color.black);
		} else {
	        searchField.setText(lastSearchText); //NOI18N
	        searchField.select(0, lastSearchText.length());
		}
    }
	
	@Override
	protected boolean changed(JComponent comp, String text, Bias bias) {
		lastSearchText=text;
		if (!(comp instanceof BrowserSubPanel)) {
			bPanel = (BrowserSubPanel)SwingUtilities.getAncestorOfClass(BrowserSubPanel.class, comp);
		} else {
			bPanel = (BrowserSubPanel)comp;
		}
		if (bPanel!=null) {
			List<FileLabel> labels = bPanel.getFileLabels();
	        boolean startingFromSelection = true;
	        int max = labels.size();
	        int increment = 0;
	        if(bias!=null)
	            increment = (bias == Position.Bias.Forward) ? 1 : -1;

	        FileLabel fl = bPanel.getLeadSelection();
	        int lastFocusedInt = -1;
	        if (fl!=null) {
	        	lastFocusedInt = labels.indexOf(fl);
	        }
	        
	        int startingRow = (lastFocusedInt + increment + max) % max;
	        if (startingRow < 0 || startingRow >= max) {
	            startingFromSelection = false;
	            startingRow = 0;
	        }
	        
	        FileLabel nextM = getNextMatch(bPanel, text, startingRow, bias);
	        if (nextM != null) {
	            bPanel.selectionChange(true, nextM, false);
	            return true;
	        } else if (startingFromSelection) {
	            nextM = getNextMatch(bPanel, text, 0, bias);
	            if (nextM != null) {
	                bPanel.selectionChange(true, nextM, false);
	                return true;
	            }
	        }
	        return false;
		}
		return false;
	}
	
    public FileLabel getNextMatch(BrowserSubPanel bPanel, String prefix, int startingRow, Position.Bias bias) {
		List<FileLabel> labels = bPanel.getFileLabels();
        int max = labels.size();
        if (prefix == null) {
            throw new IllegalArgumentException();
        }
        if (startingRow < 0 || startingRow >= max) {
            throw new IllegalArgumentException();
        }
        if(ignoreCase)
            prefix = prefix.toUpperCase();

        // start search from the next/previous element from the
        // selected element
        int increment = (bias==null || bias == Position.Bias.Forward) ? 1 : -1;
        int row = startingRow;
        do {
        	String text = labels.get(row).getText();
            if(ignoreCase)
                text = text.toUpperCase();
            if (text.indexOf(prefix)!=-1) {
                return labels.get(row);
            }
            row = (row + increment + max) % max;
        } while (row != startingRow);
        return null;
    }

	@Override
	public void keyPressed(KeyEvent ke) {
		super.keyPressed(ke);
		if (bPanel!=null && ke.getKeyCode()==KeyEvent.VK_ENTER) {
			bPanel.openSelectedFile();
		}
	}

	@Override
	public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
		super.popupMenuWillBecomeInvisible(e);
		bPanel=null;
	}
}
